package cn.thoughtworks.service.impl;

import cn.thoughtworks.model.Size;
import cn.thoughtworks.service.Verify;

/**
 * 连通性错误
 *
 * @author briarbear
 * @blog https://briarbear.github.io
 * @create 2018-07-27-15:45
 */
public class MazeError implements Verify {
    /**
     * 验证连通性错误
     * @param size 道路网格大小
     * @param command 输入命令
     * @return 验证结果
     */
    public boolean verify(Size size, String command) {

        String[] arr1 = command.split(";");

        for (int i = 0; i < arr1.length; i++) {
            String[] arr2 = arr1[i].split(" ");

            int[] n1 = new int[2];
            int[] n2 = new int[2];
            n1[0] = Integer.valueOf(arr2[0].split(",")[0]);
            n1[1] = Integer.valueOf(arr2[0].split(",")[1]);
            n2[0] = Integer.valueOf(arr2[1].split(",")[0]);
            n2[1] = Integer.valueOf(arr2[1].split(",")[1]);

            if (n1[0] == n2[0]){
                if (!(n1[1]-n2[1]==1||n1[1]-n2[1]==-1))
                    return false;
                continue;
            }
            if (n1[1] == n2[1]){
                if (!(n1[0]-n2[0]==1||n1[0]-n2[0]==-1))
                    return false;
                continue;
            }

            return false;
        }

        return true;
    }
}
